﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Month
{
    /// <summary>
    /// 盈利或亏损所占的Tick
    /// </summary>
    public class ServiceGroupMonthWinOrLoseTick : Benefit.Interface.Month.IGroupMonthWinOrLoseTick
    {
        Benefit.DB.DBManager db = null;
        public ServiceGroupMonthWinOrLoseTick(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        public void Delete(int tradeHistoryId)
        {
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            //  TradeHistory th = new TradeHistory();
            TradeHistory th = ith.GetModel(tradeHistoryId);
            var query = db.GroupMonthWinOrLoseTick.Where(a => a.Month == th.MPdate).Where(a => a.Year == th.YPdate);
            foreach (Models.Month.GroupMonthWinOrLoseTick tick in query)
            {
                db.GroupMonthWinOrLoseTick.Remove(tick);
            }
            db.SaveChanges();
        }

        public void InitGroupMonthWinOrLoseTick(int tradeHistoryId, bool win)
        {
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            //  TradeHistory th = new TradeHistory();
            TradeHistory th = ith.GetModel(tradeHistoryId);
            Interface.Sys.IDayGroupOperators idgo = new Sys.ServiceDayGroupOperators(db);
            Interface.View.IOperatorMonthTick iomt = new View.ServiceOperatorMonthTick(db);
            var groups = db.Groups.ToList();

            foreach (Models.Sys.Groups g in groups)
            {
                List<Models.View.OperatorMonthTick> allticks = new List<Models.View.OperatorMonthTick>();
     
                List<Operator> operators = idgo.GetGroupOperators(g.Id, Convert.ToDateTime(th.YPdate + "-" + th.MPdate + "-" + th.DPdate));
                foreach (Operator op in operators)
                {
     
                    //  Models.View.OperatorMonthTick tick = new Models.View.OperatorMonthTick();
                    List<Models.View.OperatorMonthTick> ticks = iomt.GetOperatorMonthWinOrLostTicks(op.Id, th.MPdate, th.YPdate, win);
                    foreach (Models.View.OperatorMonthTick t in ticks)
                    {
                        var query = allticks.Where(a => a.Tick == t.Tick).Where(a => a.Win == win);
                        if (query.Count() == 0)
                        {
                            allticks.Add(t);
                        }
                        else
                        {
                            query.First().Count += t.Count;
                        }
                    }
                }
                foreach (Models.View.OperatorMonthTick tick in allticks)
                {
                    Models.Month.GroupMonthWinOrLoseTick t = new Models.Month.GroupMonthWinOrLoseTick();
                    t.GroupId = g.Id;
                    t.Month = th.MPdate;
                    t.Year = th.YPdate;
                    t.Win = win;
                    t.Tick = tick.Tick;
                    t.Count = tick.Count;
                    var query = db.GroupMonthWinOrLoseTick.Where(a => a.GroupId == g.Id).Where(a => a.Month == th.MPdate).Where(a => a.Year == th.YPdate).Where(a => a.Win == win).Where(a => a.Tick == tick.Tick);
                    if (query.Count() > 0)
                    {
                        query.First().Count += tick.Count;
                    }
                    else
                    {
                        db.GroupMonthWinOrLoseTick.Add(t);
                    }
                }
            }
            db.SaveChanges();
        }

        public List<Models.Month.GroupMonthWinOrLoseTick> GetGroupMonthWinOrLoseTick(int groupId, int month, int year, bool win)
        {
            var query = db.GroupMonthWinOrLoseTick.Where(a => a.GroupId == groupId).Where(a => a.Month == month).Where(a => a.Year == year).Where(a => a.Win == win);
            return query.ToList();
        }

        public List<Models.Month.GroupMonthWinOrLoseTick> GetSortedGroupMonthWinOrLoseTick(int groupId, int month, int year, bool win)
        {
            var query = db.GroupMonthWinOrLoseTick.Where(a => a.GroupId == groupId).Where(a => a.Month == month).Where(a => a.Year == year).Where(a => a.Win == win).OrderBy(a => a.Tick);

            List<Models.Month.GroupMonthWinOrLoseTick> ticks = new List<Models.Month.GroupMonthWinOrLoseTick>();
            ticks.Add(query.Where(a => a.Tick == 1).First());
            ticks.Add(query.Where(a => a.Tick == 2).First());
            ticks.Add(query.Where(a => a.Tick == 3).First());
            ticks.Add(query.Where(a => a.Tick == 4).First());
            ticks.Add(query.Where(a => a.Tick == 5).First());
            ticks.Add(query.Where(a => a.Tick == 6).First());
            ticks.Add(query.Where(a => a.Tick == 7).First());
            ticks.Add(query.Where(a => a.Tick == 8).First());
            ticks.Add(query.Where(a => a.Tick == 9).First());
            ticks.Add(query.Where(a => a.Tick == 10).First());
            var _query = query.Where(a => a.Tick > 10);
            Models.Month.GroupMonthWinOrLoseTick t = new Models.Month.GroupMonthWinOrLoseTick();
            t.Tick = 11;
            t.Count = _query.Sum(a => a.Count);
            ticks.Add(t);
            return ticks;
        }



        public bool HasSettlemented(int groupId, int month, int year, bool win)
        {
            var query = db.GroupMonthWinOrLoseTick.Where(a => a.GroupId == groupId).Where(a => a.Month == month).Where(a => a.Year == year).Where(a => a.Win == win).OrderBy(a => a.Tick);
            if (query.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}